package com.dmlll.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmlll.VO.*;

import com.dmlll.common.DataGridView;
import com.dmlll.common.PinyinUtils;
import com.dmlll.common.ResultObj;
import com.dmlll.config.ExcelRead;
import com.dmlll.config.ToExcel;
import com.dmlll.domain.*;
import com.dmlll.service.DrugMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dmLll
 * @since 2021-03-31
 */
@RestController
public class DrugMessageController {

    @Autowired
    private DrugMessageService drugMessageService;
    @RequestMapping("addDrugMessage")
    public ResultObj addDrugMessage(DrugMessage drugMessage){
        try {
            QueryWrapper<DrugMessage> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("common_name",drugMessage.getCommonName())
                    .eq("dosage_form",drugMessage.getDosageForm())
                    .eq("size",drugMessage.getSize())
                    .eq("unit",drugMessage.getUnit())
                    .eq("factor",drugMessage.getFactor())
                    .eq("production_name",drugMessage.getProductionName())
                    .eq("commodity_name",drugMessage.getCommodityName());
            List<DrugMessage> list = drugMessageService.list(queryWrapper);
            if(list.size()>0){
                return ResultObj.ADD_ERROR_HAVE;
            }
            if(drugMessage.getTransaction()==null||drugMessage.getTransaction()==0){
                drugMessage.setTransaction(1);
            }
            drugMessage.setSerialNumber(UUID.randomUUID().toString().substring(0,6));
            drugMessage.setCommodityPinyin(PinyinUtils.getPinyin(drugMessage.getCommodityName()));
            drugMessageService.save(drugMessage);
        } catch (Exception e) {
            return ResultObj.ADD_ERROR;
        }
        return ResultObj.ADD_SUCCESS;
    }

    @RequestMapping("/selectCatalogue")
    @ResponseBody
    public PageInfo<ZjsVO>selectCatalogue(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
            @RequestParam(value = "serialNumber",required = false)String serialNumber,
            @RequestParam(value = "commonName",required = false)String commonName,
            @RequestParam(value = "dosageForm",required = false)String dosageForm,
            @RequestParam(value = "size",required = false)String size,
            @RequestParam(value = "unit",required = false) String unit,
            @RequestParam(value = "factor",required = false)Integer factor,
            @RequestParam(value = "category",required = false) String category
    ){
       PageInfo<ZjsVO> list = drugMessageService.selectCatalogue(pageNum,pageSize,serialNumber,commonName,dosageForm,size,unit,factor,category);
        return list;
    }

    @RequestMapping("/exportAll")
    @ResponseBody
    public void exportAll(HttpServletResponse response,@RequestParam(value ="idList",required = false,defaultValue = "")Integer[] idList){
        List<ZjsVO>list = drugMessageService.findByIdList(idList);
        List<List<String>> lists = new ArrayList<>();
        for (int i = 0;i<list.size();i++){
            ZjsVO zjsVO = list.get(i);
            List<String> list1 = new ArrayList<>();
            list1.add(zjsVO.getId().toString());
            list1.add(zjsVO.getSerialNumber());
            list1.add(zjsVO.getCommonName());
            list1.add(zjsVO.getDosageForm());
            list1.add(zjsVO.getSize());
            list1.add(zjsVO.getUnit());
            list1.add(zjsVO.getFactor().toString());
            list1.add(zjsVO.getCategory());
            list1.add(zjsVO.getDataName());
            lists.add(list1);
        }
        String[] heads={"编号","药品品目号","通用名","剂型","规格","单位","转换系数","药品类别","状态"};
        String filename = "药品目录表";
        new ToExcel<ZjsVO>().excel(filename,heads,response,lists);
    }

    @RequestMapping("/selectList")
    @ResponseBody
    public void selectList(HttpServletResponse response){
        List<ZjsinformationVO> list = new ArrayList<>();
        list = drugMessageService.selectZjsList();
        List<List<String>> lists = new ArrayList<>();
        for (int i = 0;i < list.size();i++){
            ZjsinformationVO zjsinformationVO = list.get(i);
            List<String> list1 = new ArrayList<>();
            list1.add(zjsinformationVO.getId().toString());
            list1.add(zjsinformationVO.getSerialNumber());
            list1.add(zjsinformationVO.getCommonName());
            list1.add(zjsinformationVO.getDosageForm());
            list1.add(zjsinformationVO.getSize());
            list1.add(zjsinformationVO.getUnit());
            list1.add(zjsinformationVO.getFactor().toString());
            list1.add(zjsinformationVO.getProductionName());
            list1.add(zjsinformationVO.getCommodityName());
            list1.add(zjsinformationVO.getWinPrice().toString());
            list1.add(zjsinformationVO.getZhiliang());
            list1.add(zjsinformationVO.getCategory());
            list1.add(zjsinformationVO.getZhuangtai());
            lists.add(list1);
        }
        String[] heads = {"编号","药品流水号","通用名","剂型","规格","单位","转换系数","生产企业","商品名称","中标价","质量层次","药品类别","状态"};
        String filename = "药品查询全部信息";
        new ToExcel<ZjsinformationVO>().excel(filename,heads,response,lists);
    }

    @RequestMapping("/selectDrugInformation")
    @ResponseBody
    public PageInfo<ZjsinformationVO>selectDrugInformation(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize,
            @RequestParam(value = "serialNumber",required = false)String serialNumber,
            @RequestParam(value = "commonName",required = false)String commonName,
            @RequestParam(value = "dosageForm",required = false)String dosageForm,
            @RequestParam(value = "size",required = false)String size,
            @RequestParam(value = "unit",required = false)String unit,
            @RequestParam(value = "factor",required = false)Integer factor,
            @RequestParam(value = "productionName",required = false)String productionName,
            @RequestParam(value = "winPriceMax",required = false)Double winPrice,
            @RequestParam(value = "commodityName",required = false)String commodityName,
            @RequestParam(value = "winPriceMin",required = false)Double winPriceMin,
            @RequestParam(value = "winPriceMax",required = false)Double winPriceMax,
            @RequestParam(value = "category",required = false)String category,
            @RequestParam(value = "quality",required = false)Integer quality,
            @RequestParam(value = "transaction",required = false)Integer transaction
    ){
      PageInfo<ZjsinformationVO> list = drugMessageService.selectDrugInformation(pageNum,pageSize,serialNumber,commonName,dosageForm,size,unit,factor,productionName,winPrice,commodityName,winPriceMin,winPriceMax,category,quality,transaction);
        return list;
    }


    @RequestMapping("/selectDrugInformationList")
    @ResponseBody
    public PageInfo<ZjsinformationVO>selectDrugInformationList(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize,
            @RequestParam(value = "serialNumber",required = false)String serialNumber,
            @RequestParam(value = "commonName",required = false)String commonName,
            @RequestParam(value = "dosageForm",required = false)String dosageForm,
            @RequestParam(value = "size",required = false)String size,
            @RequestParam(value = "unit",required = false)String unit,
            @RequestParam(value = "factor",required = false)Integer factor,
            @RequestParam(value = "productionName",required = false)String productionName,
            @RequestParam(value = "winPriceMax",required = false)Double winPrice,
            @RequestParam(value = "commodityName",required = false)String commodityName,
            @RequestParam(value = "winPriceMin",required = false)Double winPriceMin,
            @RequestParam(value = "winPriceMax",required = false)Double winPriceMax,
            @RequestParam(value = "category",required = false)String category,
            @RequestParam(value = "quality",required = false)Integer quality,
            @RequestParam(value = "transaction",required = false)Integer transaction
    ){
        PageInfo<ZjsinformationVO> list = drugMessageService.selectDrugInformationList(pageNum,pageSize,serialNumber,commonName,dosageForm,size,unit,factor,productionName,winPrice,commodityName,winPriceMin,winPriceMax,category,quality,transaction);
        return list;
    }

    @RequestMapping("/insertIdList")
    @ResponseBody
    public List<ProviderDrugCatalogControl> insertIdList(Integer[] idList){
                return drugMessageService.insertIdList(idList);
    }


    @RequestMapping("/insertPurchase")
    @ResponseBody
    public int insertPurchase(@RequestParam("hospitalPurchaseCatalogs") String hospitalPurchaseCatalogs){
        List<HospitalPurchaseCatalog> object= JSONObject.parseArray(hospitalPurchaseCatalogs, HospitalPurchaseCatalog.class);
        HospitalPurchaseCatalog[] objects=new HospitalPurchaseCatalog[object.size()];
        for (int i = 0; i <objects.length ; i++) {
            objects[i]=object.get(i);
        }
        return drugMessageService.insertPurchase(objects);
    }

    @RequestMapping("/selectMaintain")
    @ResponseBody
    public PageInfo<ZjsinformationVO>selectMaintain(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize,
            @RequestParam(value = "serialNumber",required = false)String serialNumber,
            @RequestParam(value = "commonName",required = false)String commonName,
            @RequestParam(value = "dosageForm",required = false)String dosageForm,
            @RequestParam(value = "size",required = false)String size,
            @RequestParam(value = "unit",required = false)String unit,
            @RequestParam(value = "factor",required = false)Integer factor,
            @RequestParam(value = "productionName",required = false)String productionName,
            @RequestParam(value = "winPriceMax",required = false)Double winPrice,
            @RequestParam(value = "commodityName",required = false)String commodityName,
            @RequestParam(value = "winPriceMin",required = false)Double winPriceMin,
            @RequestParam(value = "winPriceMax",required = false)Double winPriceMax,
            @RequestParam(value = "category",required = false)String category,
            @RequestParam(value = "quality",required = false)Integer quality,
            @RequestParam(value = "transaction",required = false)Integer transaction
    ){
        PageInfo<ZjsinformationVO> list = drugMessageService.selectMaintain(pageNum, pageSize,serialNumber,commonName,dosageForm,size,unit,factor,productionName,winPrice,commodityName,winPriceMin,winPriceMax,category,quality,transaction);
        return list;
    }


    @RequestMapping("/delectMaintain")
    @ResponseBody
    public int delectMaintain(@RequestParam(value = "idList")Integer[] idList){
        return drugMessageService.delectMaintain(idList);
    }


    @RequestMapping("/selectMaintainIdList")
    @ResponseBody
    public void selectMaintainIdList(HttpServletResponse response,@RequestParam("idList")Integer[] idList){
        List<ZjsinformationVO> list = drugMessageService.selectMaintainIdList(idList);
        List<List<String>> lists = new ArrayList<>();
        for(int i = 0;i < list.size();i++){
        ZjsinformationVO zjsinformationVO = list.get(i);
        List<String> list1 = new ArrayList<>();
        list1.add(zjsinformationVO.getId().toString());
        list1.add(zjsinformationVO.getSerialNumber());
        list1.add(zjsinformationVO.getCommonName());
        list1.add(zjsinformationVO.getDosageForm());
        list1.add(zjsinformationVO.getSize());
        list1.add(zjsinformationVO.getUnit());
        list1.add(zjsinformationVO.getFactor().toString());
        list1.add(zjsinformationVO.getProductionName());
        list1.add(zjsinformationVO.getCommodityName());
        list1.add(zjsinformationVO.getWinPrice().toString());
        list1.add(zjsinformationVO.getCategory());
        list1.add(zjsinformationVO.getZhuangtai());
        list1.add(zjsinformationVO.getProviderName());
        list1.add(zjsinformationVO.getProviderNameZhuangTai());
        lists.add(list1);
        }
        String[] heads={"编号","药品流水号","通用名","剂型","规格","单位","转换系数","生产企业","商品名称","中标价","药品类别","药品状态","供应商","供货状态"};
        String filename = "医院药品维护目录";
        new ToExcel<ZjsinformationVO>().excel(filename,heads,response,lists);
    }


    @RequestMapping("/selectProviderIdName")
    @ResponseBody
    public List<Provider> selectProviderIdName(){
        return drugMessageService.selectProviderIdName();
    }


    @RequestMapping("/selectSalesReturnCatalogue")
    @ResponseBody
    public PageInfo<ZjsSalesReturnVO> selectSalesReturnCatalogue(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "8") Integer pageSize,
             ZjsSalesReturnVO zjsSalesReturnVO){
        PageInfo<ZjsSalesReturnVO> list = drugMessageService.selectSalesReturnCatalogue(zjsSalesReturnVO,pageNum,pageSize);
        return list;
    }

    @RequestMapping("/selectGoodsNote")
    @ResponseBody
    public PageInfo<ZjsGoodsNoteVO> selectGoodsNote(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "8") Integer pageSize,
            ZjsGoodsNoteVO zjsGoodsNoteVO){
        PageInfo<ZjsGoodsNoteVO> list = drugMessageService.selectGoodsNote(zjsGoodsNoteVO,pageNum,pageSize);
        return list;
    }

    @RequestMapping("/insertGoodsNote")
    @ResponseBody
    public int insertGoodsNote(HospitalOutput hospitalOutput){
        return drugMessageService.insertGoodsNote(hospitalOutput);
    }

    @RequestMapping("/selectIdReturn")
    @ResponseBody
    public List<ZjsGoodsNoteVO> selectIdReturn(@RequestParam(value = "Sid") Integer Sid){
        return drugMessageService.selectIdReturn(Sid);
    }


    @RequestMapping("/selectAddquantity")
    @ResponseBody
    public PageInfo<ZjsAddquantityVO> selectAddquantity(
            ZjsAddquantityVO zjsAddquantityVO,
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize
            ){
        PageInfo<ZjsAddquantityVO> pageInfo = drugMessageService.selectAddquantity(zjsAddquantityVO,pageNum,pageSize);
        return pageInfo;
    }

    @RequestMapping("/insertAddquantity")
    @ResponseBody
    public int insertAddquantity(@RequestParam("hospitalOutputDetail")String hospitalOutputDetail){
        List<HospitalOutputDetail> object= JSONObject.parseArray(hospitalOutputDetail, HospitalOutputDetail.class);
        HospitalOutputDetail[] objects=new HospitalOutputDetail[object.size()];
        for (int i = 0; i <objects.length;i++) {
            objects[i]=object.get(i);
        }
        return drugMessageService.insertAddquantity(objects);
    }

    @RequestMapping("/selectInsertValue")
    @ResponseBody
    public List<PurchaseDrupDetail>selectInsertValue(@RequestParam(value = "idList")Integer[] idList){
        return drugMessageService.selectInsertValue(idList);
    }

    @RequestMapping("/delectReturn")
    @ResponseBody
    public int delectReturn(@RequestParam(value = "idList")Integer[] idList){
        return drugMessageService.delectReturn(idList);
    }

    @RequestMapping("/selectFinal")
    @ResponseBody
    public PageInfo<ZjsFinalStatementVO> selectFinal(
            ZjsFinalStatementVO zjsFinalStatementVO,
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize
    ){
        PageInfo<ZjsFinalStatementVO> pageInfo = drugMessageService.selectFinal(zjsFinalStatementVO,pageNum,pageSize);
        return pageInfo;
    }

    @RequestMapping("/insertFinal")
    @ResponseBody
    public int insertFinal(HospitalClose hospitalClose){
        return drugMessageService.insertFinal(hospitalClose);
    }

    @RequestMapping("/updateQuantity")
    @ResponseBody
    public int updateQuantity(@RequestParam("id")Integer id){
        return drugMessageService.updateQuantity(id);
    }


    @RequestMapping("/delectSalesDrug")
    @ResponseBody
    public int delectSalesDrug(@RequestParam("idList")Integer[] idList){
        return drugMessageService.delectSalesDrug(idList);
    }

    @RequestMapping("/selectClearing")
    @ResponseBody
    public PageInfo<ZjsClearingDrugs> selectClearing(
            ZjsClearingDrugs zjsClearingDrugs,
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize
            ){
        PageInfo<ZjsClearingDrugs> pageInfo = drugMessageService.selectClearing(zjsClearingDrugs,pageNum,pageSize);
        return pageInfo;
    }

    @RequestMapping("/selectFinalParticulars")
    @ResponseBody
    public List<ZjsSelectClearingVO> selectFinalParticulars(@RequestParam(value = "id")Integer id){
          return drugMessageService.selectFinalParticulars(id);
    }

    @RequestMapping("/updateCloseState")
    @ResponseBody
    public int updateCloseState(@RequestParam(value = "id")Integer id){
        return drugMessageService.updateCloseState(id);
    }

    @RequestMapping("/deleteFinalDrug")
    @ResponseBody
    public int deleteFinalDrug(@RequestParam(value = "idList")Integer[] idList){
        return drugMessageService.deleteFinalDrug(idList);
    }

    @RequestMapping("/selectPurchase")
    @ResponseBody
    public PageInfo<ZjsPurchaseOrdersVO> selectPurchase(
            ZjsPurchaseOrdersVO zjsPurchaseOrdersVO,
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize
    ){
        PageInfo<ZjsPurchaseOrdersVO> pageInfo = drugMessageService.selectPurchase(zjsPurchaseOrdersVO,pageNum,pageSize);
        return pageInfo;
    }


    @RequestMapping("/selectPurchaseByIdList")
    @ResponseBody
    public void selectPurchaseByIdList(HttpServletResponse response,@RequestParam(value = "idList",required = false,defaultValue = "")Integer[] idList){
        List<ZjsPurchaseOrdersVO> list = drugMessageService.selectPurchaseByIdList(idList);
        List<List<String>> lists = new ArrayList<>();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(int i= 0; i < list.size();i++){
            ZjsPurchaseOrdersVO zjsPurchaseOrdersVO = list.get(i);
            List<String> list1 = new ArrayList<>();
            list1.add(zjsPurchaseOrdersVO.getId().toString());
            list1.add(zjsPurchaseOrdersVO.getHospitalName());
            list1.add(zjsPurchaseOrdersVO.getPurchaseNum());
            list1.add(zjsPurchaseOrdersVO.getPurchaseName());
            list1.add(zjsPurchaseOrdersVO.getDataName());
            list1.add(format.format(zjsPurchaseOrdersVO.getBeginTime()));
            list1.add(format.format(zjsPurchaseOrdersVO.getOverTime()));
            list1.add(format.format(zjsPurchaseOrdersVO.getCreateTime()));
            list1.add(format.format(zjsPurchaseOrdersVO.getSubmitTime()));
            list1.add(format.format(zjsPurchaseOrdersVO.getAuditorTime()));
            lists.add(list1);
            }
        String[] heads={"序号","采购医院 ","采购单编号","采购单名称","采购状态","开始采购时间","结束采购时间","建单时间","提交时间","审核时间"};
        String filename = "采购单统计表";
        new ToExcel<ZjsPurchaseOrdersVO>().excel(filename,heads,response,lists);
        }




    @RequestMapping("/selectSupplier")
    @ResponseBody
    public PageInfo<ZjsSupplierVO> selectSupplier(
            ZjsSupplierVO zjsSupplierVO,
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "14")Integer pageSize
    ){
        PageInfo<ZjsSupplierVO> pageInfo = drugMessageService.selectSupplier(zjsSupplierVO,pageNum,pageSize);
        return pageInfo;
    }

    @RequestMapping("/selectSupplierByIdList")
    @ResponseBody
    public void selectSupplierByIdList(HttpServletResponse response,@RequestParam(value = "idList",required = false,defaultValue = "")Integer[] idList){
        List<ZjsSupplierVO> list = drugMessageService.selectSupplierByIdList(idList);
        List<List<String>> lists = new ArrayList<>();
        for(int i= 0; i < list.size();i++){
            ZjsSupplierVO zjsSupplierVO = list.get(i);
            List<String> list1 = new ArrayList<>();
            list1.add(zjsSupplierVO.getId()==null?"":zjsSupplierVO.getId().toString());
            list1.add(zjsSupplierVO.getProviderName()==null?"":zjsSupplierVO.getProviderName());
            list1.add(zjsSupplierVO.getDataName()==null?"":zjsSupplierVO.getDataName());
            list1.add(zjsSupplierVO.getProviderName()==null?"":zjsSupplierVO.getProviderName());
            list1.add(zjsSupplierVO.getPurchasePrice()==null?"":zjsSupplierVO.getPurchasePrice().toString());
            list1.add(zjsSupplierVO.getSongHuoLiang()==null?"":zjsSupplierVO.getSongHuoLiang().toString());
            list1.add(zjsSupplierVO.getSongHuoJinE()==null?"":zjsSupplierVO.getSongHuoJinE().toString());
            list1.add(zjsSupplierVO.getReturnsTheAmount()==null?"":zjsSupplierVO.getReturnsTheAmount().toString());
            list1.add(zjsSupplierVO.getOutputPrice()==null?"":zjsSupplierVO.getOutputPrice().toString());
            list1.add(zjsSupplierVO.getCloseNum()==null?"":zjsSupplierVO.getCloseNum().toString());
            list1.add(zjsSupplierVO.getClosePrice()==null?"":zjsSupplierVO.getClosePrice().toString());
            lists.add(list1);
        }
        String[] heads={"序号","供应商 ","采购状态","订货量","订货金额","送货量","送货金额","退货量","退货金额","结算量","结算金额"};
        String filename = "供应商统计表";
        new ToExcel<ZjsSupplierVO>().excel(filename,heads,response,lists);
    }



        @RequestMapping("updateDrugMessage")
        public ResultObj updateDrugMessage(DrugMessage drugMessage) {
            try {
                QueryWrapper<DrugMessage> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("common_name", drugMessage.getCommonName())
                        .eq("dosage_form", drugMessage.getDosageForm())
                        .eq("size", drugMessage.getSize())
                        .eq("unit", drugMessage.getUnit())
                        .eq("factor", drugMessage.getFactor())
                        .eq("production_name", drugMessage.getProductionName())
                        .eq("commodity_name", drugMessage.getCommodityName());
                List<DrugMessage> list = drugMessageService.list(queryWrapper);
                drugMessageService.updateById(drugMessage);
            } catch (Exception e) {
                return ResultObj.UPDATE_ERROR;
            }
            return ResultObj.UPDATE_SUCCESS;
        }
        @RequestMapping("loadAllDrugMessage")
        public DataGridView loadAllDrugMessage(DrugMessageVO drugMessageVO) {
            IPage<DrugMessage> page = new Page<>(drugMessageVO.getPage(), drugMessageVO.getLimit());
            QueryWrapper<DrugMessage> wrapper = new QueryWrapper<>();
            wrapper.like(StringUtils.isNotBlank(drugMessageVO.getSerialNumber()), "serial_number", drugMessageVO.getSerialNumber())
                    .like(StringUtils.isNotBlank(drugMessageVO.getCommonName()), "common_name", drugMessageVO.getCommonName())
                    .like(StringUtils.isNotBlank(drugMessageVO.getDosageForm()), "dosage_form", drugMessageVO.getDosageForm())
                    .like(StringUtils.isNotBlank(drugMessageVO.getSize()), "size", drugMessageVO.getSize())
                    .like(StringUtils.isNotBlank(drugMessageVO.getUnit()), "unit", drugMessageVO.getUnit())
                    .eq(drugMessageVO.getFactor() != null, "factor", drugMessageVO.getFactor())
                    .like(StringUtils.isNotBlank(drugMessageVO.getProductionName()), "production_name", drugMessageVO.getProductionName())
                    .like(StringUtils.isNotBlank(drugMessageVO.getCommodityName()), "commodity_name", drugMessageVO.getCommodityName())
                    .le(drugMessageVO.getWinPrice() != null, "win_price", drugMessageVO.getMaxPrice())
                    .ge(drugMessageVO.getWinPrice() != null, "win_price", drugMessageVO.getMinPrice())
                    .eq(drugMessageVO.getDrugCategory() != null && drugMessageVO.getDrugCategory() > 0, "drug_category", drugMessageVO.getDrugCategory())
                    .eq(drugMessageVO.getQuality() != null && drugMessageVO.getQuality() > 0, "quality", drugMessageVO.getQuality())
                    .eq(drugMessageVO.getTransaction() != null && drugMessageVO.getTransaction() > 0, "transaction", drugMessageVO.getTransaction());
            IPage<DrugMessage> page1 = drugMessageService.page(page, wrapper);
            return new DataGridView(page1.getTotal(), page1.getRecords());
        }
        @RequestMapping("deleteDrugMessage")
        public ResultObj deleteDrugMessage(DrugMessage drugMessage){
            try {
                drugMessageService.removeById(drugMessage);
            } catch (Exception e) {
                return ResultObj.DELETE_ERROR;
            }
            return ResultObj.DELETE_SUCCESS;
        }
        @RequestMapping("inputDrugMessage")
        public ResultObj input(MultipartFile file) throws ParseException {
            //解析文件
            List<List<String>> lists = ExcelRead.readExcel(file);
            List<DrugMessage> drugMessages = new ArrayList<>();
            for (int i = 1; i < lists.size(); i++) {
                for (int j = 0; j < 7; j++) {
                    if (lists.get(i).get(j) == null || lists.get(i).get(j) == "") {
                        return new ResultObj(-1, "请完善必填信息", 0);
                    }
                }
                DrugMessage drugMessage = new DrugMessage();

                drugMessage.setCommonName(lists.get(i).get(0));
                drugMessage.setDosageForm(lists.get(i).get(1));
                drugMessage.setSize(lists.get(i).get(2));
                drugMessage.setFactor(Integer.valueOf(lists.get(i).get(3)));
                drugMessage.setProductionName(lists.get(i).get(4));
                drugMessage.setCommodityName(lists.get(i).get(5));
                drugMessage.setUnit(lists.get(i).get(6));
                try {

                    drugMessage.setWinPrice(lists.get(i).get(7) == null ? null : Double.valueOf(lists.get(i).get(7)));
                    drugMessage.setApprovalNumber(lists.get(i).get(8) == null ? "" : lists.get(i).get(8));
                    drugMessage.setApprovalDate(lists.get(i).get(9) == null ? null : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(lists.get(i).get(9)));
                    drugMessage.setIsImport(lists.get(i).get(10) == null ? null : Integer.valueOf(lists.get(i).get(10)));
                    drugMessage.setPackages(lists.get(i).get(11));
                    drugMessage.setPackageUnit(lists.get(i).get(12));
                    drugMessage.setNewPrice(lists.get(i).get(13) == null ? null : Double.valueOf(lists.get(i).get(13)));
                    drugMessage.setPriceSource(lists.get(i).get(14));
                    drugMessage.setQuality(Integer.valueOf(lists.get(i).get(15)));
                    drugMessage.setQualityDescription(lists.get(i).get(16));
                    drugMessage.setHaveReport(lists.get(i).get(17) == null ? null : Integer.valueOf(lists.get(i).get(17)));
                    drugMessage.setReportNum(lists.get(i).get(18));
                    drugMessage.setReportDate(lists.get(i).get(19) == null ? null : new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").parse(lists.get(i).get(19)));
                    drugMessage.setProductionDescription(lists.get(i).get(20));
                } catch (Exception e) {
                    return new ResultObj(-1, "您的信息格式错误", 0);
                }
                drugMessages.add(drugMessage);
            }
            int i = 0;

            for (DrugMessage drugMessage : drugMessages) {
                ResultObj resultObj = this.addDrugMessage(drugMessage);
                if (resultObj.getCode() == -1) {
                    return new ResultObj(-1, resultObj.getMsg() + ",导入成功" + i + "，导入失败" + (lists.size() - i - 1), 0);
                }
                i++;
            }
            return new ResultObj(200,"导入成功"+i +"，导入失败"+(lists.size() -i -1),0);
        }



        @RequestMapping("downloadDrugMessage")
        public void downloadDrugMessage(HttpServletResponse response, HttpServletRequest request) throws IOException {
            try {
                String path = this.getClass().getClassLoader().getResource("mould/drugMessage.xlsx").getPath();
                //获取要下载的模板名称
                String fileName = "drugMessage.xlsx";
                //设置要下载的文件的名称
                response.setHeader("Content-disposition", "attachment;fileName=" + fileName);
                //通知客服文件的MIME类型
                response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                //获取文件的路径
                FileInputStream input = new FileInputStream(path);
                OutputStream out = response.getOutputStream();
                byte[] b = new byte[2048];
                int len;
                while ((len = input.read(b)) != -1) {
                    out.write(b, 0, len);
                }
                //修正 Excel在“xxx.xlsx”中发现不可读取的内容。是否恢复此工作薄的内容？如果信任此工作簿的来源，请点击"是"
                response.setHeader("Content-Length", String.valueOf(input.getChannel().size()));
                input.close();
            } catch (Exception ex) {

            }
        }


}


